package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.controller.api.param.ArticleColumnEditParam;
import com.dmap.knowledge.controller.api.param.ArticleColumnParam;
import com.dmap.knowledge.dao.ArticleColumnMapper;
import com.dmap.knowledge.po.ArticleColumn;
import com.dmap.knowledge.po.ArticleColumnExample;
import com.dmap.knowledge.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class ArticleColumnServiceImpl implements ArticleColumnService {
    @Resource
    private ArticleColumnMapper articleColumnMapper;
    @Resource
    private SceneService sceneService;
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleExtensionService articleExtensionService;
    @Resource
    private ArticleColumnExtensionService articleColumnExtensionService;
    
    @Override
    public ArticleColumn selectArticleColumnById(long id) {
        return articleColumnMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public List<ArticleColumn> selectArticleColumnAll() {
        ArticleColumnExample articleColumnExample = new ArticleColumnExample();
        articleColumnExample.setOrderByClause("sort asc,create_time desc");
        return articleColumnMapper.selectByExample(articleColumnExample);
    }
    
    @Override
    public int insertArticleColumn(ArticleColumnParam articleColumnParam) {
        ArticleColumn articleColumn = new ArticleColumn();
        articleColumn.setLang(articleColumnParam.getLang().toUpperCase());
        articleColumn.setIconUrl(articleColumnParam.getIconUrl());
        articleColumn.setCreateTime(System.currentTimeMillis() / 1000);
        articleColumn.setUpdateTime(System.currentTimeMillis() / 1000);
        articleColumn.setKeyName(articleColumnParam.getKey());
        articleColumn.setStatus(articleColumnParam.getStatus());
        articleColumn.setSort(articleColumnParam.getSort());
        articleColumn.setName(articleColumnParam.getName());
        articleColumn.setDescription(articleColumnParam.getComment());
        articleColumn.setSceneId(Long.parseLong(articleColumnParam.getSceneId()));
        return articleColumnMapper.insert(articleColumn);
    }
    
    @Override
    public int deleteArticleColumn(long articleColumnId) {
        //如果column的栏目有文章就不能删除
        if (articleExtensionService.countArticleByColumn(null,articleColumnId) != 0) {
            return 0;
        }
        ArticleColumn articleColumn = articleColumnMapper.selectByPrimaryKey(articleColumnId);
        if (articleColumn == null) {
            return 0;
        }
        ArticleColumnExample articleColumnExample = new ArticleColumnExample();
        articleColumnExample.createCriteria().andIdEqualTo(articleColumnId);
        return articleColumnMapper.deleteByExample(articleColumnExample);
    }
    
    @Override
    public int updateArticleColumn(ArticleColumnEditParam articleColumnEditParam) {
        ArticleColumn articleColumnOne = articleColumnMapper.selectByPrimaryKey(Long.parseLong(articleColumnEditParam.getColumnId()));
        //如果为空就返回0
        if (articleColumnOne == null) {
            return 0;
        }
        ArticleColumn articleColumn = new ArticleColumn();
        articleColumn.setId(Long.parseLong(articleColumnEditParam.getColumnId()));
        articleColumn.setLang(articleColumnEditParam.getLang().toUpperCase());
        articleColumn.setIconUrl(articleColumnEditParam.getIconUrl());
        articleColumn.setUpdateTime(System.currentTimeMillis() / 1000);
        articleColumn.setKeyName(articleColumnEditParam.getKey());
        articleColumn.setStatus(articleColumnEditParam.getStatus());
        articleColumn.setName(articleColumnEditParam.getName());
        articleColumn.setDescription(articleColumnEditParam.getComment());
        articleColumn.setSceneId(Long.parseLong(articleColumnEditParam.getSceneId()));
        return articleColumnMapper.updateByPrimaryKeySelective(articleColumn);
    }
    
    @Override
    public int countStatusColumn(long articleColumnId) {
        ArticleColumn articleColumnOne = articleColumnMapper.selectByPrimaryKey(articleColumnId);
        if(articleColumnOne==null){
            return 0;
        }
        //1代表已经启用
        if(articleColumnOne.getStatus()==1){
            return 1;
        }
        return 0;
    }
    
    @Override
    public List<ArticleColumn> selectArticleColumnNotOn() {
        ArticleColumnExample articleColumnExample = new ArticleColumnExample();
        ArticleColumnExample.Criteria criteria = articleColumnExample.createCriteria();
        //没有启用的
        criteria.andStatusNotEqualTo(1);
        return articleColumnMapper.selectByExample(articleColumnExample);
    }
    
    
    @Override
    public int deleteColumnList(String columnIds) {
        List<String> columnidList = Arrays.asList(columnIds.split(","));
        int count = 0;
        for (String columnId : columnidList) {
            count += deleteArticleColumn(Long.parseLong(columnId));
        }
        if (count == columnidList.size()) {
            return 1;
        }
        return 0;
    }
   
}
